package com.stlm2.controller;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.GoodSpecification;
import com.stlm2.dems.entity.Stages;
import com.stlm2.dems.entity.User;
import com.stlm2.dems.service.GoodSpecificationService;
import com.stlm2.dems.service.StagesService;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONObject;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author：fuchunyu
 * Date：2017/10/23
 * Description：
 */
@Controller
@RequestMapping("/stagesC")
public class StagesController {

    @Resource
    private GoodSpecificationService goodSpecificationService;
    @Resource
    private UserDao userDao;
    @Resource
    private StagesService stagesService;

    @RequestMapping(value="/listView",produces="application/json;charset=UTF-8")
    public ModelAndView listView(ModelAndView modelAndView){
        modelAndView.setViewName("/stages/list");
        return modelAndView;
    }
    @ResponseBody
    @RequestMapping(value = "searchGoodsStages.json",produces = "application/json;charset=utf-8")
    public String searchGoodsStages(ModelMap model,Integer goodsId, String page){
        if(goodsId==null)
            return CodeContent.returnFailure("商品ID不能为空！");
        PageView pageview =new  PageView();
        int pageNo = 1;
        int pageSize=20;
        if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
            pageNo = NumberUtils.toInt(page);
        }
        Stages stages=new Stages();
        stages.setGoodsId(goodsId);
        pageview =stagesService.getPage(pageNo,pageSize,stages);
        if (pageview != null){
            model.put("datas",stagesService.convert(pageview.getRecords()));
            model.put("recordsTotal", pageview.getRowCount());
            model.put("totalPage", pageview.getPageCount());
            model.put("pageNow", pageview.getPageNow());
        }
        model.put("code",1);
        model.put("msg","success");
        return  new JSONObject(model).toString();
    }

    @ResponseBody
    @RequestMapping(value = "searchPropertiesStages.json",produces = "application/json;charset=utf-8")
    public String searchPropertiesStages(String propertiesIds, String goodsId){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("goodid", goodsId);
        map.put("propertiesid", propertiesIds);
        GoodSpecification goodSpecification = goodSpecificationService.getGoodSpecification(map);
        if (goodSpecification == null)
            return CodeContent.returnFailure("没有找到此商品规格！");
        Stages stages=new Stages();
        stages.setSpecificationId(goodSpecification.getId());
        List<Stages> list=stagesService.getList(-1,-1,stages);
        return CodeContent.returnSuccess(stagesService.convert(list));
    }

    @ResponseBody
    @RequestMapping(value = "/addStages.json",produces = "application/json;charset=UTF-8")
    public String addStages(String token, Integer goodId, String propertiesIds,  BigDecimal prepayment, Integer periods, BigDecimal instalmentAmount){
        User user=userDao.getByToken(token);
        if(user==null)
            return CodeContent.returnLogin();
        Stages stages=new Stages();
        if(goodId==null&&goodId<=0)
            return CodeContent.returnFailure("非法的商品ID！");
        if(prepayment==null)
            return CodeContent.returnFailure("首付金额不能为空！");
        if(periods==null)
            return CodeContent.returnFailure("期数不能为空！");
        if(instalmentAmount==null)
            return CodeContent.returnFailure("分期金额不能为空！");

        if(StringUtils.isNotEmpty(propertiesIds)) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("goodid", goodId);
            map.put("propertiesid", propertiesIds);
            GoodSpecification goodSpecification = goodSpecificationService.getGoodSpecification(map);
            if (goodSpecification == null)
                return CodeContent.returnFailure("没有找到此商品规格！");
            stages.setSpecificationId(goodSpecification.getId());
        }else {
            stages.setGoodsId(goodId);
        }
        stages.setPrepayment(prepayment);
        stages.setPeriods(periods);
        stages.setInstalmentAmount(instalmentAmount);
        stages.setState(1);
        stages.setCreateTime(new Date());
        stages.setCreateUserId(user.getId());

        stagesService.addEntity(stages);
        return CodeContent.returnSuccess(null);
    }


    @ResponseBody
    @RequestMapping(value = "/detailsStages.json",produces = "application/json;charset=UTF-8")
    public String detailsStages(Integer id){
        Stages stages  = stagesService.findEntityById(id);
        return CodeContent.returnSuccess(stagesService.buiderModel(stages));
    }

    @ResponseBody
    @RequestMapping(value = "/updateStages.json",produces = "application/json;charset=UTF-8")
    public String updateStages(String id,BigDecimal prepayment, Integer periods, BigDecimal instalmentAmount,Integer state,String token){
        User user=userDao.getByToken(token);
        if(user==null)
            return CodeContent.returnLogin();
        if(!NumberUtils.isDigits(id))
            return CodeContent.returnFailure("非法的ID！");
        Stages stages=stagesService.findEntityById(Integer.parseInt(id));
       if(prepayment!=null)
           stages.setPrepayment(prepayment);
       if(periods!=null)
           stages.setPeriods(periods);
       if(instalmentAmount!=null)
           stages.setInstalmentAmount(instalmentAmount);
       if(state!=null)
           stages.setState(state);

        stagesService.updateEntity(stages);

        return CodeContent.returnSuccess(null);
    }
}
